package cn.wolfcode.trip.base.domain;

import cn.wolfcode.trip.base.util.JSONUtil;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

@Setter
@Getter
public class Region extends BaseDomain{

    public static final int STATE_NORMAL = 0;
    public static final int STATE_HOT = 1;

    private String name; //地区名

    private Region parent; // 父级地区

    private Integer state = STATE_NORMAL;  // 状态

    /**
     * 将Region对象域中的属性转化为treeview支持的格式
     * @return
     */
    public Map<String, Object> toMap() {
        Map<String,Object> map = new HashMap<>();
        map.put("text",name);
        map.put("id",this.getId());
        // 为子地区添加 可点击的 +
        map.put("lazyLoad",true);
        map.put("json",getJsonString());
        if (state == STATE_HOT) {
            map.put("tags",new String[]{"推荐"});
        }
        return map;
    }

    public String getJsonString(){
        Map<String,Object> map  = new HashMap<>();
        map.put("id",getId());
        map.put("name",name);

        // 上级地区信息
        if (parent != null) {
            map.put("pid",parent.getId());
            map.put("pname",parent.getName());
        }
        map.put("state",state);
        return JSONUtil.toJSONString(map);
    }
}